package com.yitiao.simple.file;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * 等待服务端上线，获取服务端信息，意在实现广播功能，有瑕疵
 */
public class ServerDiscoveryClient {
    public static void main(String[] args) {
        try {
            DatagramSocket socket = new DatagramSocket(9999);
            socket.setBroadcast(true);

            byte[] recvData = new byte[1024];
            DatagramPacket packet = new DatagramPacket(recvData, recvData.length);
            socket.receive(packet);

            String receivedInfo = new String(packet.getData()).trim();
            System.out.println("Received server info: " + receivedInfo);

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
